package at.fhj.itm10.ticketmaster.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.entities.Order;

/**
 * @author 	Kevin Reinmueller
 * @since	November 2012
 */

public class OrderDAO extends AbstractDAO {
	
	private static final String PRIMARYKEY_NAME = "id";
	private static final String SQL_INSERT ="INSERT INTO `order` (`fk_customer_id`, `order_date`, `payment_date`, `is_paid`) VALUES (?,?,?,?)";
	private static final String SQL_UPDATE = "UPDATE `order` SET `fk_customer_id` = ?, `order_date` = ?, `payment_date` = ?, `is_paid` = ? WHERE `id` = ?";
	private static final String SQL_DELETE = "DELETE FROM `order` WHERE `id` = ?";
	private static final String SQL_FINDBYID = "SELECT * FROM `order` WHERE `id` = ?";
	private static final String SQL_FINDALL = "SELECT * FROM `order`";
	private Logger logger = Logger.getLogger(OrderDAO.class);
	
	//	--
	//	-- Tabellenstruktur für Tabelle `order`
	//	--
	//
	//	CREATE TABLE IF NOT EXISTS `order` (
	//	  `id` int(11) NOT NULL AUTO_INCREMENT,
	//	  `fk_user_id` int(11) NOT NULL,
	//	  `order_date` date NOT NULL,
	//	  `payment_date` date NOT NULL,
	//	  `is_paid` int(11) NOT NULL,
	//	  PRIMARY KEY (`id`)
	//	) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
	//
	//	-- --------------------------------------------------------
	
	public OrderDAO(Connection connection) throws DAOException {
		super(connection);
	}
	
	/**
	 * {@inheritDoc}
	 */	
	public void insert(Order entity) throws DAOException {
		logger.debug("OrderDAO.insert(Order = " + entity.toString() + ")");
		checkForValidEntity(entity);
		PreparedStatement statement = prepareStatement(SQL_INSERT);

		try {
			statement.setLong(1, entity.getFk_customer_id());
			statement.setString(2, convertDateToString(entity.getOrder_date()));
			statement.setString(3, convertDateToString(entity.getPayment_date()));
			statement.setBoolean(4, entity.isIs_paid());
		} catch (Exception e) {
			throw new DAOException(e);
		}

		super.processInsert(statement, entity);
	}

	/**
	 * {@inheritDoc}
	 */	
	public void update(Order entity) throws DAOException {
		logger.debug("OrderDAO.update(Order = " + entity.toString() + ")");
		checkForValidEntity(entity);
		PreparedStatement statement = prepareStatement(SQL_UPDATE);
		
		try {
			statement.setLong(1, entity.getFk_customer_id());
			statement.setString(2, convertDateToString(entity.getOrder_date()));
			statement.setString(3, convertDateToString(entity.getPayment_date()));
			statement.setBoolean(4, entity.isIs_paid());
			statement.setLong(5, entity.getId());
		} catch (Exception e) {
			throw new DAOException(e);
		}

		super.processUpdate(statement, entity);
	}

	/**
	 * {@inheritDoc}
	 */	
	public void delete(Order entity) throws DAOException {
		logger.debug("OrderDAO.delete(Order = " + entity.toString() + ")");
		checkForValidEntity(entity);

		// Entity gets deleted by its id automatically, you just need
		// one placeholder in your DELETE query
		super.doDelete(prepareStatement(SQL_DELETE), entity);
	}

	public Order findById(long id) throws DAOException {
		logger.debug("OrderDAO.findById(Order = " + id + ")");
		Order entity = (Order) retrieveEntity(id);
		if (entity != null) {
			return entity;
		} else {
			// Do not set anything for the find-one query, as this is
			// done in super.doFindOne()
			return (Order) super.doFindOne(id, prepareStatement(SQL_FINDBYID));
		}
	}

	/**
	 * {@inheritDoc}
	 */	
	public List<Order> findAll() throws DAOException {
		logger.debug("OrderDAO.findAll()");
		return super.doFindMore(PRIMARYKEY_NAME, prepareStatement(SQL_FINDALL));
	}
	
	/**
	 * {@inheritDoc}
	 */
	protected Order fillEntityByResult(ResultSet resultSet) throws DAOException {
		logger.debug("OrderDAO.insert(Order ResultSet = " + resultSet.toString() + ")");
		Order order = new Order();
		
		try {
			order.setId(resultSet.getLong(PRIMARYKEY_NAME));
			order.setFk_customer_id(resultSet.getLong("fk_customer_id"));
			order.setIs_paid(resultSet.getBoolean("is_paid"));
			order.setOrder_date(convertStringToDate(resultSet.getString("order_date")));
			order.setPayment_date(convertStringToDate(resultSet.getString("payment_date")));
		} catch (Exception e) {
			throw new DAOException(e);
		}

		return order;
	}
}
